package com.kgcx.schedule.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kgcx.common.constant.Cons;
import com.kgcx.core.exception.BusinessException;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.dbutils.core.mapper.BaseMapper;
import com.kgcx.dbutils.core.model.ParameterMap;
import com.kgcx.schedule.domain.ScheduleReserveCfgDetailDomain;
import com.kgcx.schedule.domain.ScheduleReserveCfgDomain;
import com.kgcx.schedule.mapper.ScheduleReserveCfgDetailMapper;
import com.kgcx.schedule.mapper.ScheduleReserveCfgMapper;

@Service
// @Transactional(rollbackFor = Exception.class)
public class ScheduleReserveCfgService extends BaseBusinessService<Long, ScheduleReserveCfgDomain> {
    // private static final Logger LOG = LoggerFactory.getLogger(ScheduleReserveCfgService.class);

    @Autowired
    private ScheduleReserveCfgMapper scheduleReserveCfgMapper;
    @Autowired
    private ScheduleReserveCfgDetailMapper scheduleReserveCfgDetailMapper;

    @Override
    protected BaseMapper<Long, ScheduleReserveCfgDomain> getMapper() {
        return scheduleReserveCfgMapper;
    }

    @Override
    // @Transactional(readOnly = true)
    public ScheduleReserveCfgDomain selectEntity(Long primaryKey) {
        ScheduleReserveCfgDomain domain = super.selectEntity(primaryKey);
        domain.setDetailList(selectDetailList(domain.getKid()));
        return domain;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertEntity(ScheduleReserveCfgDomain entity) {
        if (!HelpUtil.isEmpty(scheduleReserveCfgMapper.selectEntityListPage(
                new ParameterMap("city_id", entity.getCity_id()).put("auto_type", entity.getAuto_type())))) {
            throw new BusinessException(1L, "已经存在该车辆类型的预约运力配置，请选择其他车辆类型！");
        }

        if (entity.getStatus() == null) {
            entity.setStatus(Cons.COMMON_STATUS_ACTIVE);
        }
        entity.setCreate_id(0L);
        entity.setCreate_time(HelpUtil.getNowTime());

        super.insertEntity(entity);

        insertDetailList(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEntity(ScheduleReserveCfgDomain entity) {
        deleteDetailList(entity.getKid());
        insertDetailList(entity);

        entity.setUpdate_id(0L);
        entity.setUpdate_time(HelpUtil.getNowTime());

        super.updateEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEntity(Long primaryKey) {
        deleteDetailList(primaryKey);

        super.deleteEntity(primaryKey);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private List<ScheduleReserveCfgDetailDomain> selectDetailList(Long reserve_cfg_id) {
        return scheduleReserveCfgDetailMapper
                .selectEntityListPage(new ParameterMap("reserve_cfg_id", reserve_cfg_id).setOrderBy("begin_time"));
    }

    private void insertDetailList(ScheduleReserveCfgDomain entity) {
        List<ScheduleReserveCfgDetailDomain> detailList = entity.getDetailList();
        if (HelpUtil.isEmpty(detailList)) {
            // return;
            throw new BusinessException(2L, "请配置运力时段明细列表！");
        }

        for (ScheduleReserveCfgDetailDomain detail : detailList) {
            detail.setReserve_cfg_id(entity.getKid());
        }
        scheduleReserveCfgDetailMapper.insertEntities(detailList);
    }

    private void deleteDetailList(Long reserve_cfg_id) {
        scheduleReserveCfgDetailMapper.deleteEntities(new ParameterMap("reserve_cfg_id", reserve_cfg_id));
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public List<ScheduleReserveCfgDetailDomain> selectDetailListPage(ParameterMap params) {
        return scheduleReserveCfgDetailMapper.selectEntityListPage(params);
    }
}
